import http from '../http';

// 商品分类接口类型定义
export interface Category {
  id?: number;
  name: string;
  parentId: number;
  level: number;
  sortOrder: number;
  icon?: string;
  image?: string;
  description?: string;
  isShow: number;
  createdAt?: string;
  updatedAt?: string;
  children?: Category[];
}

export interface CategoryListParams {
  page?: number;
  pageSize?: number;
  parentId?: number;
  level?: number;
  isShow?: number;
  keyword?: string;
}

export interface CategoryListResponse {
  list: Category[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

export interface CategoryStats {
  total: number;
  show: number;
  hide: number;
  levelStats: Array<{
    level: number;
    count: number;
  }>;
}

export class CategoryApi {
  /**
   * 创建商品分类
   */
  static createCategory(data: Omit<Category, 'id' | 'createdAt' | 'updatedAt'>) {
    return http.post<Category>('/api/categories', data);
  }

  /**
   * 获取分类列表
   */
  static getCategoryList(params?: CategoryListParams) {
    return http.get<CategoryListResponse>('/api/categories', { params });
  }

  /**
   * 获取分类树形结构
   */
  static getCategoryTree(isShow?: number) {
    return http.get<Category[]>('/api/categories/tree', { 
      params: isShow !== undefined ? { isShow } : {} 
    });
  }

  /**
   * 根据ID获取分类详情
   */
  static getCategoryById(id: number) {
    return http.get<Category>(`/api/categories/${id}`);
  }

  /**
   * 更新分类
   */
  static updateCategory(id: number, data: Partial<Category>) {
    return http.put<Category>(`/api/categories/${id}`, data);
  }

  /**
   * 删除分类
   */
  static deleteCategory(id: number) {
    return http.delete(`/api/categories/${id}`);
  }

  /**
   * 批量删除分类
   */
  static deleteCategories(ids: number[]) {
    return http.post('/api/categories/batch-delete', { ids });
  }

  /**
   * 更新分类显示状态
   */
  static updateCategoryStatus(id: number, isShow: number) {
    return http.patch<Category>(`/api/categories/${id}/status`, { isShow });
  }

  /**
   * 获取分类统计信息
   */
  static getCategoryStats() {
    return http.get<CategoryStats>('/api/categories/stats');
  }

  /**
   * 获取所有可见分类
   */
  static getAllVisibleCategories() {
    return http.get<Category[]>('/api/categories/tree', { 
      params: { isShow: 1 } 
    });
  }
}